package com.module04.work.demo05;


import java.io.*;
import java.net.Socket;

//客户端
public class Client1 {
    private final static String fileSrc = "/Users/wanggege/Desktop/ClientData/c1/";

    public static void main(String[] args) {
        Socket socket = null;
        BufferedReader br = null;
        try {
            System.out.println("欢迎进入Java学习交流群！本群内可以聊天交友，支持上传下载学习资料哦！");
            System.out.println("输入sendFile:+文件名即可发送文件");
            System.out.print("请输入用户名:");
            br = new BufferedReader(new InputStreamReader(System.in));
            String name = br.readLine();
            //1、指定ip + 端口 建立连接
            socket = new Socket("127.0.0.1", 8888);
            //2、客户端收发消息
            new SendMsgThread(socket, name).start();
            new ReceiveMsgThread(socket).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //向服务器发送消息
    static class SendMsgThread extends Thread {
        private Socket socket;
        private DataOutputStream dos = null;
        private BufferedReader br = null;
        private String name;

        public SendMsgThread(Socket socket, String name) throws IOException {
            this.socket = socket;
            this.name = name;
            br = new BufferedReader(new InputStreamReader(System.in));
            dos = new DataOutputStream(socket.getOutputStream());
            this.sendMsg(name);
        }

        //接收输入的消息
        public String getMsg() throws IOException {
            String msg = br.readLine();
            if (msg.startsWith("sendFile:")) {
                String port = String.valueOf(msg.split(":")[1]);
                File file = new File(fileSrc + port + ".txt");
                if (!file.exists()) {
                    System.out.println("文件不存在，请重新输入！");
                    getMsg();
                }
            }
            return msg;
        }

        //消息发送
        public void sendMsg(String msg) throws IOException {
            dos.writeUTF(msg);
            dos.flush();

        }

        @Override
        public void run() {
            try {
                while (true) {
                    String msg = getMsg();
                    if (msg.startsWith("sendFile:")) {
                        new SendFileThreead(socket, msg).start();
                    }
                    this.sendMsg(msg);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //向服务器发送文件
    static class SendFileThreead extends Thread {
        private Socket socket;
        private DataInputStream dis = null;
        private DataOutputStream dos = null;
        private String msg = "";
        File file = null;

        public SendFileThreead(Socket socket, String msg) throws IOException {
            this.socket = socket;
            this.msg = msg;
            String port = String.valueOf(msg.split(":")[1]);
            file = new File(fileSrc + port + ".txt");
            dis = new DataInputStream(new FileInputStream(file));
            dos = new DataOutputStream(socket.getOutputStream());
        }

        //获取本地文件并上传到服务器
        public void sendFile(String msg) {
            try {
                byte[] buff = new byte[1024];
                int res = 0;
                while ((res = dis.read(buff)) != -1) {
                    dos.write(buff, 0, res);
                }
                dos.flush();
                System.out.println("文件上传成功");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            if (!msg.equals("")) {
                this.sendFile(msg);
            }
        }
    }

    //接收服务器发来的消息
    static class ReceiveMsgThread extends Thread {
        private Socket socket;
        private DataInputStream dis = null;

        public ReceiveMsgThread(Socket socket) throws IOException {
            this.socket = socket;
            dis = new DataInputStream(socket.getInputStream());
        }

        public String receiveMsg() {
            String msg = "";
            try {
                msg = dis.readUTF();
                return msg;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return msg;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    String msg = receiveMsg();
                    if (msg.startsWith("系统消息") && msg.endsWith("发来的文件成功！")) {
                        new ReceiveFileThread(socket).start();
                        System.out.println(msg);
                    } else if (!msg.equals("")) {
                        System.out.println(msg);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //接收服务器发来的文件
    static class ReceiveFileThread extends Thread {
        private Socket socket;
        private DataInputStream dis = null;
        private DataOutputStream dos = null;

        public ReceiveFileThread(Socket socket) throws IOException {
            this.socket = socket;
        }

        public void receiveFile() {
            try {
                long fileName = System.currentTimeMillis();
                File file = new File(fileSrc + fileName + ".txt");
                dis = new DataInputStream(socket.getInputStream());
                dos = new DataOutputStream(new FileOutputStream(file));
                byte[] buff = new byte[1024];
                int res = 0;
                while ((res = dis.read(buff)) != -1) {
                    dos.write(buff, 0, res);
                }
                dos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            receiveFile();
        }
    }
}
